//
//  DCChooseTableVIew.m
//  Driver
//
//  Created by 大橙子 on 2018/11/7.
//  Copyright © 2018 Tomous. All rights reserved.
//

#import "DCChooseTableVIew.h"

#define HeaderHeight 30
#define CellHeight 30
@interface DCChooseTableVIew ()<UITableViewDataSource,UITableViewDelegate>

@end
@implementation DCChooseTableVIew
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _choosedArr = [[NSMutableArray alloc]initWithCapacity:0];
        [self CreateTable];
    }
    return self;
}
-(void)CreateTable{
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewStylePlain;
    [self addSubview:_tableView];
    
    UIView * headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.width, HeaderHeight)];
    UIButton *chooseIcon = [UIButton buttonWithType:UIButtonTypeCustom];
    chooseIcon.tag = 10;
    [chooseIcon setImage:[UIImage imageNamed:@"方框"] forState:UIControlStateNormal];
    [chooseIcon setImage:[UIImage imageNamed:@"选择-方框"] forState:UIControlStateSelected];
    chooseIcon.userInteractionEnabled = NO;
    [headerView addSubview:chooseIcon];
    [chooseIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(headerView.mas_left);
        make.top.equalTo(headerView.mas_top).offset(5);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(20);
    }];
    UILabel * HeaderTitleLab = [[UILabel alloc]init];
    HeaderTitleLab.text = @"全选";
    [headerView addSubview:HeaderTitleLab];
    [HeaderTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(chooseIcon.mas_right).offset(5);
        make.top.equalTo(headerView.mas_top).offset(0);
        make.width.mas_offset(100*WIDTHSCALE6);
        make.height.mas_offset(30);
    }];
    
    UIButton * chooseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    chooseBtn.frame = CGRectMake(0, 0, headerView.frame.size.width, headerView.frame.size.height);
    [chooseBtn addTarget:self action:@selector(ChooseAllClick:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:chooseBtn];
    
    _tableView.tableHeaderView = headerView;
}
-(void)ChooseAllClick:(UIButton *)button{
    _ifAllSelecteSwitch = YES;
    UIButton * chooseIcon = (UIButton *)[_tableView.tableHeaderView viewWithTag:10];
    chooseIcon.selected = !_ifAllSelected;
    _ifAllSelected = !_ifAllSelected;
    
    NSMutableArray *array = [NSMutableArray new];
    for (DCDispatchModel *model in _dataArr) {
//        [array addObject:model.VIN];
    }
    if (_ifAllSelected) {
        [_choosedArr removeAllObjects];
        [_choosedArr addObjectsFromArray:array];
    }
    else{
        [_choosedArr removeAllObjects];
    }
    [_tableView reloadData];
    _block(@"All",_choosedArr);
    
}
#pragma UITableViewDelegate - UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return CellHeight;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *CellID = [NSString stringWithFormat:@"%ld-Cell",indexPath.row];
    DCProgressTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellID];
    if (!cell) {
        cell = [[DCProgressTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
    }
    cell.model = self.dataArr[indexPath.row];
    if (_ifAllSelecteSwitch) {
        [cell updateCellWithState:_ifAllSelected];
        if (indexPath.row == _dataArr.count-1) {
            _ifAllSelecteSwitch  = NO;
        }
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DCProgressTableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell updateCellWithState:!cell.isSelected];
    
    DCDispatchModel *model = self.dataArr[indexPath.row];
    if (cell.isSelected) {
//        [_choosedArr addObject:model.VIN];
    }
    else{
//        [_choosedArr removeObject:model.VIN];
    }
    
    if (_choosedArr.count<_dataArr.count) {
        _ifAllSelected = NO;
        UIButton * chooseIcon = (UIButton *)[_tableView.tableHeaderView viewWithTag:10];
        chooseIcon.selected = _ifAllSelected;
    }
//    _block(model.VIN,_choosedArr);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
